//
//  UnsignedViewController.m
//  Manager
//
//  Created by 彭凯敏 on 15/8/18.
//  Copyright (c) 2015年 52xiaoluo. All rights reserved.
//

#import "UnsignedViewController.h"
#import "RoomCell.h"
#import "SignInfoViewController.h"


#import "Viewroom.pb.h"
#import "ViewRoomLogic.h"
#import "ViewOrderModel.h"

#import <MJRefresh.h>
#import "QueueLogic.h"
#import "UnsignInfoViewController.h"

@interface UnsignedViewController ()
{
    NSMutableArray *dataArray;
}
@property (strong, nonatomic) UITableView *tableView;


@end

@implementation UnsignedViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self baseCtlSetTitle:@"未签约"];
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-64) style:UITableViewStylePlain];
    [_tableView setLineType:TableViewLineTypeAll];
    _tableView .delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView setLineType:TableViewLineTypeAll];
    
    __weak __typeof(self) weakSelf = self;
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [weakSelf queryRecordRequestNew];
        
    }];
    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [weakSelf queryRecordRequestMore];
    }];
    
    dataArray = [[NSMutableArray alloc]init];
    
    [self reloadData];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(orderDataChanged:) name:Notifi_Order_Data object:nil];
    
    [self queryRecordRequestNew];
}


#pragma mark - request
- (void)queryRecordRequestNew
{
    [[ViewRoomLogic shareInstance]loadNewer:@"5"];
    
}
- (void)queryRecordRequestMore
{
    [[ViewRoomLogic shareInstance]loadMore:@"5"];
    
}
- (void)queryRecordRequestOrigin
{
    QueryOrderReqBuilder *builder = [[QueryOrderReqBuilder alloc]init];
    
    [builder setStatus:@"5"];
    
    [[ViewRoomLogic shareInstance]queryOrderReqWithBuild:builder.build];
    
}
- (void)orderDataChanged:(NSNotification *)noti
{
    DLog(@"isMainThread = %d",[NSThread isMainThread]);
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [_tableView.footer endRefreshing];
        [_tableView.header endRefreshing];
        if (noti.object == nil) {
         return ;   
        }
        else if ([noti.object isKindOfClass:[ErrorModel class]]) {
            
            ErrorModel *model = noti.object;
            [UIViewCategory FailShowWith:model];
            return ;
        }
        else
        {
            NSString *status = noti.object;
            if ([status isEqualToString:@"5"]) {
                [self reloadData];
            }
        }
    });
}

-(void) reloadData {
    [dataArray setArray:[[ViewRoomLogic shareInstance] getOrdeModelArrayByStatus:@"5"]];
    [_tableView reloadDataAndisNil:dataArray.count nitText:@"无未签约记录"];
}

#pragma mark - UITableViewDelegate & UITableViewDataSource
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    UnsignInfoViewController *vc = [[UnsignInfoViewController alloc]initWithNibName:IS320? @"UnsignInfoViewController_5":@"UnsignInfoViewController" bundle:nil];
    ViewOrderModel *model = dataArray[indexPath.row];
    vc.orderInfo = model.order;
    [self.navigationController pushViewController:vc animated:YES];

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return dataArray.count;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"RoomCell";
    RoomCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell)
    {

        [tableView registerNib:[UINib nibWithNibName:@"RoomCell_6" bundle:nil] forCellReuseIdentifier:CellIdentifier];
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        
    }
    
    ViewOrderModel *model = dataArray[indexPath.row];
    [cell setCellOrder:model.order];
    
    return cell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 176;
    
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
#ifdef __IPHONE_8_0
    
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        
        [cell setLayoutMargins:UIEdgeInsetsZero];
        
    }
#endif
    
    
}


@end
